﻿Imports DAO
Imports DTO
Imports BUS

Public Class frmTraCuuDocGia : Inherits DevComponents.DotNetBar.Office2007Form

    Private Sub cbTenDocGia_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbTenDocGia.CheckedChanged
        txtTenDocGia.Enabled = cbTenDocGia.Checked
    End Sub

    Private Sub cbEmail_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbEmail.CheckedChanged
        txtEmail.Enabled = cbEmail.Checked
    End Sub

    Private Sub cbNo_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbNo.CheckedChanged
        txtNo.Enabled = cbNo.Checked
    End Sub

    Private Sub cbDiaChi_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbDiaChi.CheckedChanged
        txtDiaChi.Enabled = cbDiaChi.Checked
    End Sub

    Private Sub cbMaDocGia_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbMaDocGia.CheckedChanged
        txtMaDocGia.Enabled = cbMaDocGia.Checked
    End Sub

    Private Sub cbNgayLapThe_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbNgayLapThe.CheckedChanged
        dtiNgayLapThe.Enabled = cbNgayLapThe.Checked
        dtiNgayLapTheDen.Enabled = cbNgayLapThe.Checked
    End Sub

    

    Private Sub cbLoaiDocGia_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbLoaiDocGia.CheckedChanged
        cboLoaiDocGia.Enabled = cbLoaiDocGia.Checked
    End Sub

    Private Sub cbNgaySinhTu_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbNgaySinhTu.CheckedChanged
        dtiNgaySinhDen.Enabled = cbNgaySinhTu.Checked
        dtiNgaySinhTu.Enabled = cbNgaySinhTu.Checked
    End Sub


    Private Sub cbNhanVienLapThe_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbNhanVienLapThe.CheckedChanged
        cboNhanVienLapThe.Enabled = cbNhanVienLapThe.Checked
    End Sub

    Private Sub cbNgayHetHan_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbNgayHetHan.CheckedChanged
        dtiNgayHetHan.Enabled = cbNgayHetHan.Checked
        dtiNgayHenHanDen.Enabled = cbNgayHetHan.Checked
    End Sub

    Private stt As Integer

    Private Sub btnTim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTim.Click
        stt = 1
        Dim docgiabus As New CDocGiaBUS()
        Dim docgiacrt As New CDocGiaCrt()
        If (cbMaDocGia.Checked = True) Then
            If (Not Integer.TryParse(txtMaDocGia.Text, docgiacrt.MaDocGia)) Then
                MessageBox.Show("Mã độc giả không hợp lệ", "Thong Bao")
                txtMaDocGia.Focus()
                Return
            End If
        End If
        docgiacrt.CheckMaDocGia = cbMaDocGia.Checked
        docgiacrt.CheckTenDocGia = cbTenDocGia.Checked
        docgiacrt.CheckEmail = cbEmail.Checked
        docgiacrt.CheckDiaChi = cbDiaChi.Checked

        docgiacrt.CheckLoaiDocGia = cbLoaiDocGia.Checked
        docgiacrt.CheckNo = cbNo.Checked
        docgiacrt.CheckNgayHetHan = cbNgayHetHan.Checked
        docgiacrt.CheckNgayLapThe = cbNgayLapThe.Checked
        docgiacrt.CheckNgaySinhTu = cbNgaySinhTu.Checked
        docgiacrt.CheckNhanVienLapThe = cbNhanVienLapThe.Checked



        If (cbTenDocGia.Checked = True) Then
            docgiacrt.TenDocGia = txtTenDocGia.Text
        End If

        If (cbEmail.Checked = True) Then
            docgiacrt.Email = txtEmail.Text
        End If


        If (cbDiaChi.Checked = True) Then
            docgiacrt.DiaChi = True
        End If

        If (cbLoaiDocGia.Checked = True) Then
            Dim loaidocgiadto As CLoaiDocGiaDTO = cboLoaiDocGia.SelectedItem
            docgiacrt.MaLoaiDocGia = loaidocgiadto.Ma
            docgiacrt.TenLoaiDocGia = loaidocgiadto.Ten
        End If

        If (cbNo.Checked = True) Then
            If (Not Integer.TryParse(txtNo.Text, docgiacrt.TongNo)) Then
                MessageBox.Show("Nợ phải là số nguyên!", "Thong Bao")
                txtNo.Focus()
                Return
            End If
        End If

        If (cbNgayHetHan.Checked = True) Then
            If (Not DateTime.TryParse(dtiNgayHetHan.Value, docgiacrt.NgayHetHanTu)) Then
                MessageBox.Show("""Ngày hết hạn từ ""chưa hợp lệ!", "Thong Bao")
                dtiNgayHetHan.Focus()
                Return
            End If

            If (Not DateTime.TryParse(dtiNgayHenHanDen.Value, docgiacrt.NgayHetHanDen)) Then
                MessageBox.Show("""Ngày hết hạn đến ""chưa hợp lệ!", "Thong Bao")
                dtiNgayHenHanDen.Focus()
                Return
            End If
        End If



        If (cbNgayLapThe.Checked = True) Then
            If (Not DateTime.TryParse(dtiNgayLapThe.Value, docgiacrt.NgayLapTheTu)) Then
                MessageBox.Show("""Ngày lập thẻ từ ""chưa hợp lệ!", "Thong Bao")
                dtiNgayLapThe.Focus()
                Return
            End If

            If (Not DateTime.TryParse(dtiNgayLapTheDen.Value, docgiacrt.NgayLapTheDen)) Then
                MessageBox.Show("""Ngày lập thẻ đến""chưa hợp lệ!", "Thong Bao")
                dtiNgayLapTheDen.Focus()
                Return
            End If
        End If


        If (cbNgaySinhTu.Checked = True) Then
            If (Not DateTime.TryParse(dtiNgaySinhTu.Value, docgiacrt.NgaySinh)) Then
                MessageBox.Show("""Ngày sinh từ ""chưa hợp lệ!", "Thong Bao")
                dtiNgaySinhTu.Focus()
                Return
            End If

            If (Not DateTime.TryParse(dtiNgaySinhDen.Value, docgiacrt.NgaySinhDen)) Then
                MessageBox.Show("""Ngày sinh đến ""chưa hợp lệ!", "Thong Bao")
                dtiNgaySinhDen.Focus()
                Return
            End If
        End If

        If (cbNhanVienLapThe.Checked = True) Then
            Dim nhanviendto As CNhanVienDTO = cboNhanVienLapThe.SelectedItem
            docgiacrt.MaNhanVienLapThe = nhanviendto.Ma
            docgiacrt.TenNhanVienLapThe = nhanviendto.HoTen
        End If


        Dim listdocgiadto As List(Of CDocGiaDTO)
        If (cbMaDocGia.Checked = True) Then
            Dim docgiadto As CDocGiaDTO = docgiabus.TraCuu(docgiacrt.MaDocGia)
            dgvDanhSachDocGia.Rows.Clear()
            If (docgiadto Is Nothing) Then
                Return
            End If
            dgvDanhSachDocGia.Rows.Add(stt, docgiadto.Ma, docgiadto.HoTen, docgiadto.NgaySinh, docgiadto.DiaChi, docgiadto.Email, docgiadto.MaLoaiDocGia, docgiadto.TenLoaiDocGia, docgiadto.NgayLapThe, docgiadto.NgayHetHan, docgiadto.TongNo, docgiadto.MaNhanVien, docgiadto.TenNhanVien)
        Else
            listdocgiadto = docgiabus.TraCuuKhiKhongBietMa(docgiacrt)
            dgvDanhSachDocGia.Rows.Clear()
            For Each docgiadto As CDocGiaDTO In listdocgiadto
                dgvDanhSachDocGia.Rows.Add(stt, docgiadto.Ma, docgiadto.HoTen, docgiadto.NgaySinh, docgiadto.DiaChi, docgiadto.Email, docgiadto.MaLoaiDocGia, docgiadto.TenLoaiDocGia, docgiadto.NgayLapThe, docgiadto.NgayHetHan, docgiadto.TongNo, docgiadto.MaNhanVien, docgiadto.TenNhanVien)
                stt += 1
            Next
        End If


    End Sub

    Private Sub frmTraCuuDocGia_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtEmail.Enabled = False
        cboLoaiDocGia.Enabled = False
        txtMaDocGia.Enabled = False
        txtTenDocGia.Enabled = False
        txtNo.Enabled = False
        dtiNgayHetHan.Enabled = False
        dtiNgayLapThe.Enabled = False
        dtiNgaySinhTu.Enabled = False
        dtiNgaySinhDen.Enabled = False
        txtDiaChi.Enabled = False
        cboNhanVienLapThe.Enabled = False
        dtiNgayHenHanDen.Enabled = False
        dtiNgayLapTheDen.Enabled = False

        Dim nhanvienbus As New CNhanVienBUS()
        Dim listnhanviendto As List(Of CNhanVienDTO)
        listnhanviendto = nhanvienbus.LoadNhanVien_BPThuThu()

        For Each nhanviendto As CNhanVienDTO In listnhanviendto
            cboNhanVienLapThe.Items.Add(nhanviendto)
        Next

        cboNhanVienLapThe.ValueMember = "Ma"
        cboNhanVienLapThe.DisplayMember = "HoTen"
        cboNhanVienLapThe.SelectedIndex = 1


        Dim loaidocgiabus As New CLoaiDocGiaBUS()
        Dim listloaidocgiadto As List(Of CLoaiDocGiaDTO)
        listloaidocgiadto = loaidocgiabus.LoadLoaiDocGia()
        For Each loaidocgiadto As CLoaiDocGiaDTO In listloaidocgiadto
            cboLoaiDocGia.Items.Add(loaidocgiadto)
        Next

        cboLoaiDocGia.ValueMember = "Ma"
        cboLoaiDocGia.DisplayMember = "Ten"
        cboLoaiDocGia.SelectedIndex = 0

    End Sub

  
    Private Sub btnHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        txtMaDocGia.Text = ""
        txtDiaChi.Text = ""
        txtEmail.Text = ""
        txtNo.Text = ""
        txtTenDocGia.Text = ""
        txtEmail.Enabled = False
        cboLoaiDocGia.Enabled = False
        txtMaDocGia.Enabled = False
        txtTenDocGia.Enabled = False
        txtNo.Enabled = False
        dtiNgayHetHan.Enabled = False
        dtiNgayLapThe.Enabled = False
        dtiNgaySinhTu.Enabled = False
        dtiNgaySinhDen.Enabled = False
        txtDiaChi.Enabled = False
        cboNhanVienLapThe.Enabled = False
        dtiNgayHenHanDen.Enabled = False
        dtiNgayLapTheDen.Enabled = False
        cboNhanVienLapThe.SelectedIndex = 1
        cboLoaiDocGia.SelectedIndex = 0

        cbEmail.Checked = False
        cbLoaiDocGia.Checked = False
        cbMaDocGia.Checked = False
        cbNgayHetHan.Checked = False
        cbNgayLapThe.Checked = False
        cbNgaySinhTu.Checked = False
        cbNhanVienLapThe.Checked = False
        cbNo.Checked = False
        cbDiaChi.Checked = False
    End Sub
End Class